package firewing.service.googletrends;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.logging.Logger;

import firewing.object.Trend;

public class GoogleTrendsExtractor {

	private static final Logger log = Logger
			.getLogger(GoogleTrendsExtractor.class.getName());

	/**
	 * Hot Searches (USA)
	 */
	private final static String TREND_LIST = "http://www.google.co.uk/trends/hottrends/atom/hourly";
	private final static String DETAIL_BASE = "http://www.google.co.uk/trends/hottrends?q=";

	/**
	 * load and parse the Google Trends atom page
	 * 
	 * @return list of trends extracted
	 */
	private static ArrayList<String> parseListPage() {
		ArrayList<String> lstReturn = new ArrayList<String>();
		try {
			// load the Google Trends atom page
			URL url = new URL(TREND_LIST);
			URLConnection conn = url.openConnection();
			conn.setAllowUserInteraction(false);
			conn.setReadTimeout(0);
			conn.setConnectTimeout(0);

			String s = "", content = "";
			BufferedReader in = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			while ((s = in.readLine()) != null) {
				content += s;
			}

			// parse keywords
			String[] arrSplit = content.toLowerCase().split("</li>|<li>");

			for (int i = 1; i < arrSplit.length; i++) {
				if (arrSplit[i].indexOf("<a href") != -1) {
					// found keywords
					String[] arrKeywordSplit = arrSplit[i].split("\"|<|>");
					lstReturn.add(arrKeywordSplit[8]);
				}
			}

			log.info("Finished parsing list page.");
		} catch (Exception e) {
			e.printStackTrace();

			log.severe("Failed to load URL.");
		}

		return lstReturn;
	}

	private static Trend parseDetailPage(String trend) {
		log.info("Started parsing detail page for: " + trend);

		Trend ret = new Trend(trend);

		try {
			// load the URL
			URL url = new URL(DETAIL_BASE + URLEncoder.encode(trend, "UTF-8"));
			URLConnection conn = url.openConnection();
			conn.setAllowUserInteraction(false);
			conn.setReadTimeout(0);
			conn.setConnectTimeout(0);

			String s = "", content = "";
			BufferedReader in = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			while ((s = in.readLine()) != null) {
				content += s;
			}

			// parse the page
			// hotness
			int pos = content.indexOf("Hotness:");
			if (pos != -1) {
				// found hotness
				int posStart = content.indexOf(">", pos);
				int posStop = content.indexOf("<", posStart);

				// extract the hotness
				ret.setHotness(content.substring(posStart + 1, posStop));
			}

			// related searches
			pos = content.indexOf("Related searches:");
			if (pos != -1) {
				// found related searches
				int posStart = content.indexOf("<br>", pos);
				int posStop = content.indexOf("<br>", posStart + 4);

				// extract the related searches
				String strRelatedSearches = content.substring(posStart + 4,
						posStop);

				String[] arrSplitRelatedSearches = strRelatedSearches
						.split(", ");

				for (int i = 0; i < arrSplitRelatedSearches.length; i++) {
					if (!arrSplitRelatedSearches[i].isEmpty()) {
						ret.addRelatedSearch(arrSplitRelatedSearches[i]);
					}
				}
			}
			// peak
			pos = content.indexOf("Peak:");
			if (pos != -1) {
				// found related searches
				int posStart = content.indexOf("<br>", pos);
				int posStop = content.indexOf("<br>", posStart + 4);

				// extract the related searches
				ret.setPeak(content.substring(posStart + 4, posStop));
			}

			log.info("Finished parsing detail page for: " + trend);

			return ret;
		} catch (Exception e) {
			e.printStackTrace();

			log.severe("Failed to load URL.");

			return null;
		}
	}

	public static ArrayList<Trend> run() {
		ArrayList<Trend> ret = new ArrayList<Trend>();

		for (String s : parseListPage()) {
			ret.add(parseDetailPage(s));
		}

		return ret;
	}
}
